// 格式化工具

// 日期格式化
export const formatDate = (date, format = 'YYYY-MM-DD') => {
  if (!date) return ''
  
  const d = new Date(date)
  if (isNaN(d.getTime())) return ''
  
  const year = d.getFullYear()
  const month = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  const hours = String(d.getHours()).padStart(2, '0')
  const minutes = String(d.getMinutes()).padStart(2, '0')
  const seconds = String(d.getSeconds()).padStart(2, '0')
  
  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds)
}

// 时间格式化
export const formatTime = (date, format = 'YYYY-MM-DD HH:mm:ss') => {
  return formatDate(date, format)
}

// 相对时间
export const formatRelativeTime = (date) => {
  if (!date) return ''
  
  const now = new Date()
  const target = new Date(date)
  const diffMs = now.getTime() - target.getTime()
  const diffMinutes = Math.floor(diffMs / (1000 * 60))
  
  if (diffMinutes < 1) return '刚刚'
  if (diffMinutes < 60) return `${diffMinutes}分钟前`
  
  const diffHours = Math.floor(diffMs / (1000 * 60 * 60))
  if (diffHours < 24) return `${diffHours}小时前`
  
  const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24))
  if (diffDays < 30) return `${diffDays}天前`
  
  const diffMonths = Math.floor(diffDays / 30)
  if (diffMonths < 12) return `${diffMonths}个月前`
  
  const diffYears = Math.floor(diffDays / 365)
  return `${diffYears}年前`
}

// 金额格式化
export const formatMoney = (amount, decimals = 2, separator = ',') => {
  if (amount === null || amount === undefined || amount === '') return '0.00'
  
  const num = parseFloat(amount)
  if (isNaN(num)) return '0.00'
  
  const parts = num.toFixed(decimals).split('.')
  parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, separator)
  
  return parts.join('.')
}

// 数字格式化（千分位）
export const formatNumber = (num, separator = ',') => {
  if (num === null || num === undefined || num === '') return '0'
  
  const number = parseFloat(num)
  if (isNaN(number)) return '0'
  
  return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, separator)
}

// 文件大小格式化
export const formatFileSize = (bytes) => {
  if (bytes === 0) return '0 B'
  
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

// 手机号格式化
export const formatPhone = (phone) => {
  if (!phone) return ''
  const str = phone.toString()
  return str.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3')
}

// 身份证号格式化
export const formatIdCard = (idCard) => {
  if (!idCard) return ''
  const str = idCard.toString()
  return str.replace(/(\d{6})(\d{8})(\d{4})/, '$1********$3')
}

// 银行卡号格式化
export const formatBankCard = (cardNo) => {
  if (!cardNo) return ''
  const str = cardNo.toString()
  return str.replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')
}

// 价格范围格式化
export const formatPriceRange = (minPrice, maxPrice) => {
  if (minPrice === maxPrice) {
    return `¥${formatMoney(minPrice)}`
  }
  return `¥${formatMoney(minPrice)} - ¥${formatMoney(maxPrice)}`
}

// 折扣格式化
export const formatDiscount = (discount) => {
  if (!discount) return ''
  const num = parseFloat(discount)
  if (isNaN(num)) return ''
  
  if (num >= 1) {
    return `${num}折`
  } else {
    return `${(num * 10).toFixed(1)}折`
  }
}

// 评分格式化
export const formatRating = (rating, maxRating = 5) => {
  if (!rating) return '0.0'
  const num = parseFloat(rating)
  if (isNaN(num)) return '0.0'
  
  return num.toFixed(1)
}

// 地址格式化
export const formatAddress = (province, city, district, detail) => {
  const parts = [province, city, district, detail].filter(Boolean)
  return parts.join(' ')
}

// 订单状态格式化
export const formatOrderStatus = (status) => {
  const statusMap = {
    1: '待付款',
    2: '待发货',
    3: '待收货',
    4: '已完成',
    5: '已取消',
    6: '退款中',
    7: '已退款'
  }
  return statusMap[status] || '未知状态'
}

// 支付方式格式化
export const formatPaymentMethod = (method) => {
  const methodMap = {
    'wechat': '微信支付',
    'alipay': '支付宝',
    'balance': '余额支付',
    'card': '银行卡'
  }
  return methodMap[method] || '未知方式'
} 